package com.cyt.sys.util;

import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.model.po.SysDictContent;
import com.cyt.sys.model.vo.SysDictViewVo;
import com.cyt.sys.util.cache.DictUtils;

import java.util.List;

/**
 * easypoi版本自定义字典
 *
 * @author taocongcong
 * @create 2019-09-12 10:29
 */
public class GlobalExcelDictHandler implements IExcelDictHandler {

    @Override
    public String toName(String dict, Object obj, String name, Object value) {
        // 导出
        return getDictName(dict, (String) value);
    }

    @Override
    public String toValue(String dict, Object obj, String name, Object value) {
        // 导入
        return getDictValue(dict, (String) value);
    }

    private String getDictName(String dict, String value) {
        String dictName = "";
        SysDictViewVo wxApplyUseType = DictUtils.getBaseDictViewVo(dict);
        if (wxApplyUseType == null) {
            return dictName;
        }
        List<SysDictContent> sysDictContentList = wxApplyUseType.getSysDictContentList();
        if (sysDictContentList == null || sysDictContentList.size() == 0) {
            return dictName;
        }
        for (SysDictContent sysDictContent : sysDictContentList) {
            if (StringUtils.equals(value, sysDictContent.getValue())) {
                dictName = sysDictContent.getName();
                break;
            }
        }
        return dictName;
    }

    private String getDictValue(String dict, String name) {
        String dictValue = "";
        SysDictViewVo wxApplyUseType = DictUtils.getBaseDictViewVo(dict);
        if (wxApplyUseType == null) {
            return dictValue;
        }
        List<SysDictContent> sysDictContentList = wxApplyUseType.getSysDictContentList();
        if (sysDictContentList == null || sysDictContentList.size() == 0) {
            return dictValue;
        }
        for (SysDictContent sysDictContent : sysDictContentList) {
            if (StringUtils.equals(name, sysDictContent.getName())) {
                dictValue = sysDictContent.getValue();
                break;
            }
        }
        return dictValue;
    }

}
